$(document).ready(function (){
	var list_url = "knowledge/blacklistip/queryInfo";
	var list_col = [
						{text:'起始IP',name:"ipStart",align:"left",searchRender : function (el){
					   		index_render_div(el ,{type:"ip"});
					    }},
						{text:'终止IP',name:"ipEnd",align:"left",searchRender : function (el){
					   		index_render_div(el ,{type:"ip"});
					    }},
						{text:'备注',name:"blackListDesc",align:"left"}
						
				   ];
	var index_oper = [
						{icon:"rh-icon rh-edit" ,text:"修改" ,aclick:edit_template_init},
					  	{icon:"rh-icon rh-delete" ,text:"删除" ,aclick:index_list_delete}
					 ];

	var create_url = "knowledge/blacklistip/addInfo";

	var update_url = "knowledge/blacklistip/updInfo";

	var delete_url = "knowledge/blacklistip/doBatchDel";

	var search_el;

	event_init();

	black_list();

	function event_init()
	{
		$("#add_btn").click(function (){
			edit_template_init();
		});

		$("#delete_btn").click(function (){
			index_list_batch_delete();
		});

		$("#refresh_btn").click(function (){
			black_list();
			if($("#edit_template").size() > 0)
			{
				$("#edit_template").umDataBind("reset");
			}
		});

		$("#search_btn").click(function(){
		if (search_el)
		{
			search_el.parent().show();
			return false;
		}
		search_tpl_init();
	});
	}

	function black_list()
	{
		g_grid.render($("#table_div"),{
			 header:list_col,
			 oper: index_oper,
			 operWidth:"100px",
			 url:list_url,
			 isLoad : true,
			 maskObj : "body",
			 hideSearch:true,
			 dbClick : detail_template_init,
			 hasBorder : false,
			 autoHeight : true,
			 showCount : true
		});
	}

	function index_list_delete(rowData)
	{
		g_dialog.operateConfirm("确认删除此记录么" ,{
			saveclick : function()
			{
				um_ajax_post({
					url : delete_url,
					paramObj : {ids : rowData.blacklistId},
					successCallBack : function(data){
						black_list();
						g_dialog.operateAlert(null ,"操作成功！");
					}
				});
			}
		});
	}

	function index_list_batch_delete()
	{
		var target_attributed_id = [];

		target_attributed_id = g_grid.getIdArray($("#table_div") ,{chk:true,attr:"blacklistId"})

		if(target_attributed_id.length == 0){
			g_dialog.operateAlert(null ,index_select_one_at_least_msg ,"error");
			return false;
		}

		target_attributed_id = target_attributed_id.join(",");

		g_dialog.operateConfirm("确认删除选中的记录么" ,{
			saveclick : function()
			{
				um_ajax_post({
					url : delete_url,
					paramObj : {ids : target_attributed_id},
					successCallBack : function(data){
						black_list();
						g_dialog.operateAlert(null ,"操作成功！");
					}
				});
			}
		});
	}

	function edit_template_init(rowData)
	{
		var title = rowData ? "黑名单IP地址库修改" : "黑名单IP地址库新增";
		$.ajax({
			type: "GET",
			url: "module/base_resource_mant/sec_event_manage/blacklist_tpl.html",
			success :function(data)
			{
				g_dialog.dialog($(data).find("[id=edit_template]"),{
					width:"600px",
					title:title,
					init:init,
					saveclick:save_click
				});
			}
		});

		function init(el)
		{
			g_validate.init(el);
			var el_startIp = el.find("[data-id=ipStart]");
			var el_endIp = el.find("[data-id=ipEnd]");
			var el_ipV6 = el.find("[data-id=ipV6]");
			el.find("[name=ipRadioStatus]").click(function (){
				if ($(this).val() == "0")
		 		{
			 		el_startIp.removeAttr("disabled");
					el_endIp.removeAttr("disabled");
			 		el_ipV6.attr("disabled" ,"disabled");
			 		el_ipV6.val("");
			 		g_validate.clear([el_ipV6]);
			 	}
			 	else
			 	{
			 		el_startIp.attr("disabled" ,"disabled");
			 		el_endIp.attr("disabled" ,"disabled");
			 		el_ipV6.removeAttr("disabled");
			 		el_startIp.val("");
					el_endIp.val("");
			 		g_validate.clear([el_startIp,el_endIp]);
			 	}
			 });

			if (rowData)
			{
				el.umDataBind("render" ,rowData);
				el.find("[name=ipRadioStatus][value="+rowData.ipRadioStatus+"]").click();
				if(rowData.ipRadioStatus == 1)
				{					
					el.find("[data-id=ipV6]").val(rowData.ipStart);
				}
				
			}

	    }

		function save_click(el ,saveObj)
		{
			if (!g_validate.validate($("#edit_template")))
 			{
 				return false;
 			}
 			if (!g_validate.ipValidate( el.find("[data-id=ipStart]"),el.find("[data-id=ipEnd]"))){
		 		return false;
		 	}

		 	if(el.find("[name=ipRadioStatus]:checked").val() == 0)
		 	{
		 		saveObj.ipRange = saveObj.ipStart + "-" + saveObj.ipEnd;
		 	}

		 	if(el.find("[name=ipRadioStatus]:checked").val() == 1)
		 	{
		 		saveObj.ipStart = saveObj.ipV6;
		 		saveObj.ipEnd = saveObj.ipV6;
		 		saveObj.ipRange = saveObj.ipV6;
		 	}

			var flag_url = create_url;
			if (rowData)
			{
				flag_url = update_url;
				saveObj.blacklistId = rowData.blacklistId;
			}
			um_ajax_post({
				url : flag_url,
				paramObj : saveObj,
				maskObj : el,
				successCallBack : function (data){
					g_dialog.operateAlert(null ,title+"成功！");
					g_dialog.hide(el);
					black_list();
				}
			});
			
		}
	}

	function detail_template_init(rowData)
	{
		$.ajax({
			type: "GET",
			url: "module/base_resource_mant/sec_event_manage/blacklist_tpl.html",
			success :function(data)
			{
				g_dialog.dialog($(data).find("[id=detail_template]"),{
					width:"450px",
					title:"黑名单IP地址库详细信息",
					isDetail:true,
					init:init
				});
			}
		});

		function init(el)
		{
			$(el).umDataBind("render" ,rowData);
			if(rowData.ipRadioStatus == 0)
			{
				el.find("[data-id=teamIp]").text(rowData.ipStart + "-" + rowData.ipEnd);
			}
			if(rowData.ipRadioStatus == 1)
			{
				el.find("[data-id=teamIp]").text(rowData.ipStart);
			}
		}
	}

	function search_tpl_init()
	{
		$.ajax({
			type: "GET",
			url: "module/base_resource_mant/sec_event_manage/blacklist_tpl.html",
			success: function(data) {
				g_dialog.dialog($(data).find("[id=edit_template]"), {
					width: "600px",
					title:"黑名单IP地址库查询",
					init:init,
					closeType:"hide",
					saveclick: save_click
				});
			}
		});

		function init(el)
		{
			el.find("input").removeAttr("validate","required");
			search_el = el;
			g_validate.init(el);

			var el_startIp = el.find("[data-id=ipStart]");
			var el_endIp = el.find("[data-id=ipEnd]");
			var el_ipv6 = el.find("[data-id=ipV6]");
			el_startIp.attr("validate","ipv4");
			el_endIp.attr("validate","ipv4");
			el_ipv6.attr("validate","ipv6");
			el.find("[name=ipRadioStatus]").click(function(){
				if($(this).val() == "0")
				{
					el_startIp.removeAttr("disabled");
					el_endIp.removeAttr("disabled");
					el_ipv6.attr("disabled","disabled");
					el_ipv6.val("");
					g_validate.clear([el_ipv6]);
				}
				else
				{
					el_startIp.attr("disabled","disabled");
					el_endIp.attr("disabled","disabled");
					el_ipv6.removeAttr("disabled");
					el_startIp.val("");
					el_endIp.val("");
					g_validate.clear([el_startIp ,el_endIp]);
				}
			});
		}

		function save_click(el,saveObj)
		{
			if(!g_validate.validate(el))
			{
				return false;
			}
			if (el.find("[name=ipRadioStatus]:checked").val() == 0)
			{
				// if (!g_validate.ipv4Validate(el.find("[data-id=ipStart]") ,el.find("[data-id=ipEnd]")))
				// {
				// 	return false;
				// }
				if (!g_validate.ipValValidate(el.find("[data-id=ipStart]") ,el.find("[data-id=ipEnd]")))
				{
					return false;
				}
			}
			g_grid.refresh($("#table_div"),{
				queryBefore : function(queryObj){
					return saveObj;
				}
			});
			g_dialog.hide(el, "hide");
		}
	}


});